package com.example.jetpack.b_viewModel_livedata

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class CounterViewModel(counterReserved: Int) : ViewModel() {

    /**
     * 外部仅可访问变量 基本用法
     */
    val counter: LiveData<Int>
        get() = _counter
    private var _counter = MutableLiveData<Int>()

    init {
        _counter.value = counterReserved
    }

    fun plusOne() {
        val count = _counter.value ?: 0
        _counter.value = count + 1
    }
}